package com.erp.module.oem

import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.view.View
import android.widget.AdapterView
import com.erp.R
import com.erp.bean.OemBean
import com.erp.module.oem.XxOem.XxOemActivity
import com.erp.myAdapter.OemAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragmengt_oem.*

class FragmentOem : BaseKFragment(),OemContract.OemView, AdapterView.OnItemClickListener, PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener, View.OnClickListener {


    var updatedialog: AlertDialog? = null
    lateinit var P: IOemP
    lateinit var adapter: OemAdapter
    lateinit var oemlist: MutableList<OemBean>
    override fun getLayoutId(): Int {
        return R.layout.fragmengt_oem
    }

    override fun initData() {

        oemlist = ArrayList()
        LvOem.onItemClickListener = this
        adapter = OemAdapter(oemlist, activity!!)
        P = IOemP( oemlist, this, activity!!)
        LvOem.adapter = adapter
        LvOem.onItemClickListener = this
        BuSousuo.setOnClickListener(this)
        Pull.setheader(true)
        Pull.setOnFooterRefreshListener(this)
        Pull.setOnHeaderRefreshListener(this)
        Pull.headerRefreshing()
    }

    override fun onClick(p0: View?) {
        when(p0){
            BuSousuo -> {
                updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                P.sousuo(EtSouSuo.text.toString())
            }
        }
    }


    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        val intent = Intent(activity, XxOemActivity::class.java)
        intent.putExtra("id", oemlist[position].id)
        startActivityForResult(intent, 3)
    }

    fun  qingkong(){
        if(isFirstLoad){
            return
        }
        Pull.headerRefreshing()
    }

    override fun xialaOk() {
        updatedialog?.dismiss()
        Pull?.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun xialaNo() {
        updatedialog?.dismiss()
        Pull?.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updatedialog?.dismiss()
        Pull?.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun shanglaNo() {
        updatedialog?.dismiss()
        Pull?.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }

    override fun onFooterRefresh(view: PullToRefreshView) {
        P.shangla()
    }
    override fun onHeaderRefresh(view: PullToRefreshView) {
        P.xiala()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                3 -> {
                    updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                    Pull.headerRefreshing()
                }
            }
        }
    }
}